package com.example.yk_lx.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.yk_lx.R;
import com.example.yk_lx.baseMVVM.BaseActivity;
import com.example.yk_lx.databinding.ActivityLoginBinding;

@Route(path = "/app/LoginActivity")
public class LoginActivity extends BaseActivity<ActivityLoginBinding,LoginViewModel> {

    @Override
    protected int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        long time = SPUtils.getInstance().getLong("time");
        if(System.currentTimeMillis()-time<5*60*1000){//登录时间小于5分钟
            ARouter.getInstance().build("/app/ImActivity").navigation();//跳转到主页面
            //更新登录时间
            SPUtils.getInstance().put("time",System.currentTimeMillis());
        }


        dataBinding.login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.login(dataBinding.etUsername.getText().toString(),dataBinding.etPassword.getText().toString());
            }
        });
        dataBinding.vv.setVideoPath("http://alimov2.a.yximgs.com/upic/2020/08/22/18/BMjAyMDA4MjIxODA1MzhfNDU1MzYxMTVfMzQ4MDQ3Njc4NTNfMV8z_b_B7782c9c48475df0674209a0d58fa8eb5.mp4?tag=1-1599265808-vdg-1-eokfxyruy0-63689f95338e1698&tt=b&bp=10000");
        dataBinding.vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                dataBinding.vv.start();
            }
        });


    }

    @Override
    protected void initData() {
        viewModel.success.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
                ARouter.getInstance().build("/app/ImActivity").navigation();
                //登录成功，保存用户名和密码以及登录的时间
                SPUtils.getInstance().put("username",dataBinding.etUsername.getText().toString());
                SPUtils.getInstance().put("name",dataBinding.etUsername.getText().toString());
                SPUtils.getInstance().put("time",System.currentTimeMillis());
            }
        });
        viewModel.fail.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
    }

    @Override
    protected Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }
}